package com.bsoft.gol.editor.controller;


import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.EditorDict;
import com.bsoft.gol.editor.editor.qo.EditorDictQo;
import com.bsoft.gol.editor.editor.request.DictReq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bsoft.gol.editor.service.EditorDictApi;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.List;

/**
 * @author lxy
 * @date 2019/6/24
 */
@Controller
@RequestMapping(value = "/api/auth/editorDict")
public class EditorDictController {

    @Autowired
    private EditorDictApi editorDictApi;

    @RequestMapping("/list")
    @ResponseBody
    public BaseResponse<List<EditorDict>> list(@RequestBody DictReq req) {

        BaseResponse<List<EditorDict>> objectBaseResponse = new BaseResponse<>();

        EditorDictQo editorDictQo = new EditorDictQo();
        BeanUtils.copyProperties(req, editorDictQo);

        List<EditorDict> list = editorDictApi.list(editorDictQo);
        objectBaseResponse.setData(list);
        return objectBaseResponse;
    }

    @RequestMapping("/codelist")
    @ResponseBody
    public BaseResponse<List<EditorDict>> codelist(@RequestBody DictReq req) {

        BaseResponse<List<EditorDict>> objectBaseResponse = new BaseResponse<>();

        EditorDictQo editorDictQo = new EditorDictQo();
        BeanUtils.copyProperties(req, editorDictQo);

        List<EditorDict> list = editorDictApi.codelist(editorDictQo);
        objectBaseResponse.setData(list);
        return objectBaseResponse;
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public BaseResponse<String> addOrUpdate(@RequestBody EditorDictQo qo) {

        BaseResponse<String> objectBaseResponse = new BaseResponse<>();


        String dictId = editorDictApi.addOrUpdate(qo);
        objectBaseResponse.setData(dictId);
        return objectBaseResponse;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse<String> delete(@RequestBody EditorDictQo qo) {

        BaseResponse<String> objectBaseResponse = new BaseResponse<>();

        if (!CommonUtils.isNull(qo.getDictId())) {
            editorDictApi.delete(qo.getDictId());
            objectBaseResponse.setMessage("删除成功");
            objectBaseResponse.setData(qo.getDictId());
        } else {
            objectBaseResponse.setCode(500);
            objectBaseResponse.setMessage("参数缺失");
        }

        return objectBaseResponse;
    }

    @RequestMapping("/state")
    @ResponseBody
    public BaseResponse<String> state(@RequestBody EditorDictQo qo) {

        BaseResponse<String> objectBaseResponse = new BaseResponse<>();

        if (!CommonUtils.isNull(qo.getDictId())) {
            editorDictApi.changeState(qo.getDictId(), qo.getDictState());
            objectBaseResponse.setMessage("设置成功");
            objectBaseResponse.setData(qo.getDictId());
        } else {
            objectBaseResponse.setCode(500);
            objectBaseResponse.setMessage("参数缺失");
        }

        return objectBaseResponse;
    }


    /**
     * 获取子类字典列表
     *
     * @param qo qo
     * @return res
     */
    @RequestMapping("/detail")
    @ResponseBody
    public BaseResponse<List<EditorDict>> detail(@RequestBody EditorDictQo qo) {

        BaseResponse<List<EditorDict>> objectBaseResponse = new BaseResponse<>();

        if (!CommonUtils.isNull(qo.getDictId())) {
            List<EditorDict> list = editorDictApi.detail(qo.getDictId());
            objectBaseResponse.setData(list);
        } else {
            objectBaseResponse.setCode(500);
            objectBaseResponse.setMessage("参数缺失");
        }

        return objectBaseResponse;
    }

    /**
     * 根据id获取详情
     *
     * @param qo qo
     * @return res
     */
    @RequestMapping("/oneDetail")
    @ResponseBody
    public BaseResponse<EditorDict> oneDetail(@RequestBody EditorDictQo qo) {

        BaseResponse<EditorDict> objectBaseResponse = new BaseResponse<>();

        if (!CommonUtils.isNull(qo.getDictId())) {
            EditorDict dict = editorDictApi.oneDetail(qo.getDictId());
            objectBaseResponse.setData(dict);
        } else {
            objectBaseResponse.setCode(500);
            objectBaseResponse.setMessage("参数缺失");
        }

        return objectBaseResponse;
    }
}
